package com.photologics.logic;

import android.graphics.Point;

public class ChangeLogic {
	private byte num, point;
	private byte[][] verticalHint = new byte[LogicHelper.LOGIC_MAXSIZE][LogicHelper.HINT_MAXSIZE];
	private byte[][] horizontalHint = new byte[LogicHelper.HINT_MAXSIZE][LogicHelper.LOGIC_MAXSIZE];
	private byte[][] sectionLogic = new byte[LogicHelper.LOGIC_MAXSIZE][LogicHelper.LOGIC_MAXSIZE];
	private byte[][] sectionState = new byte[LogicHelper.LOGIC_MAXSIZE][LogicHelper.LOGIC_MAXSIZE];
	private byte[][] wholeLogic;
	private byte[][] wholeState;
	private int verticalHintSize, horizontalHintSize;
	private int numOfSuccess;
	
	
	public ChangeLogic() {
		numOfSuccess = 0;
		wholeLogic = LogicHelper.logicInfo.getValue().clone();
		wholeState = LogicHelper.logicInfo.getState().clone();
	}
	
	public byte[][] getLogicValue(int x, int y) {
		Point base = new Point(x * LogicHelper.LOGIC_MAXSIZE, y * LogicHelper.LOGIC_MAXSIZE);
		int a, b;
		a = -1;
		b = 0;
		for (int i = base.x ; i < base.x+LogicHelper.LOGIC_MAXSIZE ; i++) {
			a++;
			for (int j = base.y ; j < base.y+LogicHelper.LOGIC_MAXSIZE ; j++) {
				sectionLogic[a][b++] = wholeLogic[i][j];
				if(wholeLogic[i][j]==1) numOfSuccess++;
			}
			b = 0;
		}
		return sectionLogic;
	}
	
	public int getNumOfSuccess() {
		return numOfSuccess;
	}
	
	public byte[][] getDbState(int x, int y){
		wholeState = LogicHelper.db.getState(LogicHelper.logicInfo.getName());
		
		Point base = new Point(x * LogicHelper.LOGIC_MAXSIZE, y * LogicHelper.LOGIC_MAXSIZE);
		int a, b;
		a = -1;
		b = 0;
		for (int i = base.x ; i < base.x+LogicHelper.LOGIC_MAXSIZE ; i++) {
			a++;
			for (int j = base.y ; j < base.y+LogicHelper.LOGIC_MAXSIZE ; j++) {
				sectionState[a][b++] = wholeState[i][j];
			}
			b = 0;
		}
		return sectionState;
	}
	
	public byte[][] getState(int x, int y) {
		Point base = new Point(x * LogicHelper.LOGIC_MAXSIZE, y * LogicHelper.LOGIC_MAXSIZE);
		int a, b;
		a = -1;
		b = 0;
		for (int i = base.x ; i < base.x+LogicHelper.LOGIC_MAXSIZE ; i++) {
			a++;
			for (int j = base.y ; j < base.y+LogicHelper.LOGIC_MAXSIZE ; j++) {
				sectionState[a][b++] = wholeState[i][j];
			}
			b = 0;
		}
		return sectionState;
		
	}

	public byte[][] getVerticalHint() {
		verticalHintSize=1;
		for (int i = 0; i < LogicHelper.LOGIC_MAXSIZE; i++) {
			point = 0;
			num = 0;
			for (int j = LogicHelper.LOGIC_MAXSIZE-1; j >= 0; j--) {
				if (sectionLogic[i][j] == 1)
					num++;
				if ((sectionLogic[i][j] == 0 && num > 0) || (j == 0 && num > 0)) {
					verticalHint[i][point++] = num;
					num = 0;
				}
				if(verticalHintSize<point)
					verticalHintSize=point;
			}
		}

		return verticalHint;
	}

	public int getVerticalHintSize() {
		return verticalHintSize;
	}

	public int getHorizontalHintSize() {
		return horizontalHintSize;
	}

	public byte[][] getHorizontalHint() {
		horizontalHintSize=1;
		for (int i = 0; i < LogicHelper.LOGIC_MAXSIZE; i++) {
			point = 0;
			num = 0;
			for (int j = LogicHelper.LOGIC_MAXSIZE-1; j >=0; j--) {
				if (sectionLogic[j][i] == 1)
					num++;
				if ((sectionLogic[j][i] == 0 && num > 0)
						|| (j == 0 && num > 0)) {
					horizontalHint[point++][i] = num;
					num = 0;
				}
			}
			if(horizontalHintSize<point)
				horizontalHintSize=point;
		}
		return horizontalHint;

	}
}
